package com.hanainfo.hanaApp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Random;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.hanainfo.db.DataBaseHelper;
import com.sun.org.apache.bcel.internal.generic.NEW;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AppHomeActivity extends Activity {
	
	
	/** Called when the activity is first created. */
	private int[] six = {0,0,0,0,0,0};
	private DataBaseHelper mOpenHelper = new DataBaseHelper(this);
	private SQLiteDatabase db;
	
	private static final String MY_TB_NAME = "test";
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main2);
	    // TODO Auto-generated method stub
	    
		
	    Button create = (Button) findViewById(R.id.create);
	    Button save = (Button) findViewById(R.id.save);
	    
	    /*//JSON URL 파싱 
	    String spec = "http://www.645lotto.net/ajax_jsonNew.asp";	
	    URL url = null;
	    URLConnection con = null;
		try{
			url= new URL(spec);
			con = url.openConnection();
			con.setUseCaches(false);
			
			String api = "";
			String line = null;
			BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
			while ((line=br.readLine())!= null){
				api = api+line + "\n";
			}
			Object ob = JSONValue.parse(api);
			JSONObject job = (JSONObject) ob;
			
			System.out.print(job.get("GRWNO"));
		}catch (Exception e) {
			// TODO: handle exception
		}*/
	    
	    save.setOnClickListener(new OnClickListener() {
	    
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView et1 = (TextView) findViewById(R.id.et1);
				TextView et2 = (TextView) findViewById(R.id.et2);
				TextView et3 = (TextView) findViewById(R.id.et3);
				TextView et4 = (TextView) findViewById(R.id.et4);
				TextView et5 = (TextView) findViewById(R.id.et5);
				TextView et6 = (TextView) findViewById(R.id.et6);
				String flag = (String) et1.getText();
				
				if(!(flag.equals(null)||flag.equals(""))){
					
					db = mOpenHelper.getWritableDatabase();
					
					ContentValues row = new ContentValues();
					row.put("first",(String) et1.getText());
					row.put("second",(String) et2.getText());
					row.put("third",(String) et3.getText());
					row.put("fourth",(String) et4.getText());
					row.put("fifth",(String) et5.getText());
					row.put("sixth",(String) et6.getText());
					
					db.insert(MY_TB_NAME, "", row);
					Toast.makeText(AppHomeActivity.this, "저장되었습니다.", Toast.LENGTH_SHORT).show();
					db.close();
				}else{
					Toast.makeText(AppHomeActivity.this, "숫자 생성 바람.", Toast.LENGTH_SHORT).show();
				}
			}
		});
	    
	    
	    create.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				for(int i=0 ; i<6 ; i++){
					Random r = new Random();
					int x = Math.abs(r.nextInt(45)+1);
					setNumber(i,x);
				}
				
				setBubble();
				
				TextView et1 = (TextView) findViewById(R.id.et1);
				TextView et2 = (TextView) findViewById(R.id.et2);
				TextView et3 = (TextView) findViewById(R.id.et3);
				TextView et4 = (TextView) findViewById(R.id.et4);
				TextView et5 = (TextView) findViewById(R.id.et5);
				TextView et6 = (TextView) findViewById(R.id.et6);
				
				et1.setText(Integer.toString(six[0]));
				et2.setText(Integer.toString(six[1]));
				et3.setText(Integer.toString(six[2]));
				et4.setText(Integer.toString(six[3]));
				et5.setText(Integer.toString(six[4]));
				et6.setText(Integer.toString(six[5]));
				
				et1.setTextColor(Color.BLACK);
				et2.setTextColor(Color.BLACK);
				et3.setTextColor(Color.BLACK);
				et4.setTextColor(Color.BLACK);
				et5.setTextColor(Color.BLACK);
				et6.setTextColor(Color.BLACK);
				
				
			}
		});
	    
	    Button next = (Button)findViewById(R.id.button1);
	    next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(AppHomeActivity.this, myBall.class);
				startActivity(i);
			}
		});
	    
	    Button last = (Button)findViewById(R.id.button2);
	    last.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent i = new Intent(AppHomeActivity.this, viewActivity.class);
				startActivity(i);
			}
		});
	}
	
	public void setNumber(int i, int x){
		int flag = 1;
		for(int y=0 ; y<i ; y++){
			if(flag==0){
				y=0;
			}
			if(this.six[y]==x){
				Random r = new Random();
				x = Math.abs(r.nextInt(45)+1);
				flag = 0;
				y--;
			}else{
				flag = 1;
			}
		}
		this.six[i]=x;
	}
	
	public void setBubble(){
		int temp;
		int flag=1;
		
		for(int y=1; y<6;y++){
			if(flag==0){
				y=1;
			}
			temp=this.six[y];
			if(this.six[y-1]>temp){
				this.six[y]=six[y-1];
				this.six[y-1]=temp;
				flag=0;
				y--;
			}else{
				flag=1;
			}
		}
	}

}
